/****** Object:  Stored Procedure [dbo].spKhachHangInsert Script Date: 6/11/2012 ******/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[spKhachHangInsert]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE [dbo].[spKhachHangInsert]
GO

/******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:					Description:
**		--------	--------				-----------------------------------
**		11/06/2012	SongTM					Innit created
*******************************************************************************/

CREATE PROCEDURE [dbo].[spKhachHangInsert]
(
	@MaKH nvarchar(6) OUTPUT,
	@TenKH nvarchar(50),
	@DiaChi nvarchar(80),
	@TinhThanh nvarchar(25),
	@QuanHuyen nvarchar(25),
	@SoDT nvarchar(15),
	@SoFax nvarchar(15),
	@MaSoThue nvarchar(15),
	@Email nvarchar(30),
	@Website nvarchar(35),
	@GhiChu nvarchar(100)
)
WITH ENCRYPTION

AS

SET NOCOUNT ON

if not exists (select * from [tbl_KhachHang])
	Set @MaKH=1
else
	Set @MaKH=(select RIGHT(MAX(MaKH),4) from [tbl_KhachHang])+1
Set @MaKH='KH'+REPLICATE('0',4-LEN(@MaKH))+@MaKH  
INSERT INTO [tbl_KhachHang]
(
	[MaKH],
	[TenKH],
	[DiaChi],
	[TinhThanh],
	[QuanHuyen],
	[SoDT],
	[SoFax],
	[MaSoThue],
	[Email],
	[Website],
	[GhiChu]
)
VALUES
(
	@MaKH,
	@TenKH,
	@DiaChi,
	@TinhThanh,
	@QuanHuyen,
	@SoDT,
	@SoFax,
	@MaSoThue,
	@Email,
	@Website,
	@GhiChu
)
GO

--GRANT EXECUTE ON [dbo].[spKhachHangInsert] TO [sa]
--GO